home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / lcppb.zip / LCPP09.ZIP / MFNPTR.CPP < prev    next >
C/C++ Source or Header  |  1991-07-08  |  1KB  |  52 lines

  1. // mfnptr.cpp -- Member function pointers
  2.  
  3. //#include <stream.hpp>
  4. #include <iostream.h>
  5. #include <iomanip.h>
  6.  
  7. class firstClass {
  8.   private:
  9.     int count;
  10.   public:
  11.     firstClass() { count = 0; }
  12.     int access(void);
  13. };
  14.  
  15. int (firstClass::*myfnptr)(void);
  16.  
  17. main()
  18. {
  19.   int i;
  20.   firstClass fc;
  21.  
  22.   cout << "\nCall access the normal way:\n";
  23.   for (i = 0; i < 9; i++)
  24. //    cout << dec(fc.access(), 8);
  25.     cout << setw(8) << dec << fc.access();
  26.  
  27.   cout << "\n\nCall access via the member function pointer\n";
  28.   myfnptr = firstClass::access;
  29.   for (i = 0; i < 9; i++)
  30. //    cout << dec((fc.*myfnptr)(), 8);
  31.     cout << setw(8) << dec << (fc.*myfnptr)();
  32.  
  33.   cout << "\n\nMember function pointer and a dynamic instance\n";
  34.   firstClass *fp = new firstClass;
  35.   for (i = 0; i < 9; i++)
  36. //    cout << dec((fp->*myfnptr)(), 8);
  37.     cout << setw(8) << dec << (fp->*myfnptr)();
  38. }
  39.  
  40. int firstClass::access(void)
  41. {
  42.   return count++;
  43. }
  44.  
  45.  
  46. // Copyright (c) 1990 by Tom Swan. All rights reserved
  47. // Revision 1.00    Date: 11/25/1990   Time: 01:45 pm
  48.  
  49. // Revision 1.01    Date: 07/08/1991   Time: 05:41 pm
  50. // Converted for Borland C++ 2.0
  51.  
  52.